package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.domain.SpecKeyDomain;
import com.ngc.mall.dto.SpecDTO;
import com.ngc.mall.mapper.SpecKeyMapper;
import com.ngc.mall.service.ISpecService;
import com.ngc.mall.service.ISpecValueService;
import com.ngc.mall.vo.SpecDetailVO;
import com.ngc.mall.vo.SpecValueVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class SpecKeyServiceImpl extends BaseServiceImpl<SpecKeyMapper, SpecKeyDomain> implements ISpecService {

    @Resource
    private ISpecValueService specValueService;

    @Override
    public List<SpecDetailVO> selectAllSpec() {
        List<SpecKeyDomain> specKeyDomainList=super.list();
        List<SpecDetailVO> specDetailVOList=new ArrayList<>();
        for (SpecKeyDomain domain :specKeyDomainList){
            List<SpecValueVO> values= specValueService.selectAllValueByKeyId(domain.getId());
            SpecDetailVO vo=SpecDetailVO
                    .builder()
                    .name(domain.getName())
                    .id(domain.getId())
                    .values(values)
                    .note(domain.getNote())
                    .unit(domain.getUnit())
                    .build();
            specDetailVOList.add(vo);
        }
        return specDetailVOList;
    }



    @Override
    public Long createSpec(SpecDTO dto) {
        SpecKeyDomain domain=SpecKeyDomain
                .builder()
                .name(dto.getName())
                .unit(dto.getUnit())
                .note(dto.getNote())
                .build();
        super.save(domain);
        Long id=domain.getId();
        return id;
    }

    @Override
    public Boolean updateSpecKeyById(SpecDTO dto, Long id) {
        SpecKeyDomain domain=SpecKeyDomain
                .builder()
                .name(dto.getName())
                .unit(dto.getUnit())
                .note(dto.getNote())
                .build();
        return super.update(Wrappers.lambdaUpdate(domain).eq(SpecKeyDomain::getId,id));
    }

}
